package com.example.springboot_practice.Config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @Description: 测试配置多数据源
 * @Author 王宝凯
 * @Date 2020/9/9
 */
@Configuration
@MapperScan(value = "com.example.springboot_practice.Mapper.MapperTwo",sqlSessionFactoryRef = "sqlSessionFactoryTest2")
public class Test2MybatisConfig {
    @Autowired
    @Qualifier("TestTwo")
    DataSource TestTwo;
    @Bean
    SqlSessionFactory sqlSessionFactoryTest2() throws Exception{
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(TestTwo);
        //数据源是自己加载xml
        factoryBean.setMapperLocations(
                // 设置mybatis的xml所在位置
                new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
        return factoryBean.getObject();
    }

    @Bean
    SqlSessionTemplate sqlSessionTemplateTest2() throws Exception{
        return new SqlSessionTemplate(sqlSessionFactoryTest2());
    }
}
